{% include 'header.html'%}
<title>Package Query</title>
<script type="text/javascript" src="{{ STATIC_URL }}js/mustache.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}js/jquery-Mustache.js"></script>
<script id="tr_build_tmpl" type="text/html">
    <tr class="plugable">
    [[#m_builds_len?]]
      <td class="th-label">Build:</td>
      <td><select id="sel_build" name="sel_build" class="fixed-field">
          [[#m_builds]]
          <option value="[[.]]" selected>[[.]]</option>
          [[/m_builds]]
          </select><span class="field-indic"></span>
      </td>
    [[/m_builds_len?]]
    </tr>
    <tr class="plugable">
      <td class="th-label">Search Package:</td>
      <td><input type="text" name="pkg_query" value="" class="fixed-field"></td>
    </tr>
    <tr class="plugable">
      <td class="th-label">Exclude Abnormal:</td>
      <td><input type="checkbox" name="include_abnormal" class="fixed-field"></td>
    </tr>
    <tr class="plugable">
      <td class="th-label"></td>
      <td><input id="btn_search_package" name="btn_showdb" type="submit" value="Search Package" style="margin-left: 10px;"/>
          <span class="field-indic"></span>
      </td>
    </tr>
</script>
<script id="tr_searched_pkgs_tmpl" type="type/html">
    <tr class="plugable searched_pkgs">
      <td class="th-label">Related Packages:</td>
      <td>
        <select id="target_pkg" name="target_pkg" class="fixed-field">
          <option value=""></option>
          [[#pkg_list]]
          <option value="[[relation__origin_package__id]]">[[relation__origin_package__name]]</option>
          [[/pkg_list]]
        </select><span class="field-indic"></span>
      </td>
    </tr>
</script>
<script id="depend_tmpl" type="text/html">
<div class="plugable searched_pkgs pkg_list" id="div_depends">
  <span>Depends</span>
  <ul>
    [[#depends]]
    <li><a href="#">[[relation__depend_package__name]]</a></li>
    [[/depends]]
    [[^depends_len?]]
    <li>Not Found</li>
    [[/depends_len?]]
  </ul>
</div>
<div class="plugable searched_pkgs pkg_list" id="div_affects">
  <span>Affects</span>
  <ul>
    [[#affects]]
    <li><a href="#">[[relation__origin_package__name]]</a></li>
    [[/affects]]
    [[^affects_len?]]
    <li>Not Found</li>
    [[/affects_len?]]
  </ul>
</div>
</script>
<script type="text/javascript">
function endLoading(obj) {
    if (obj) {
        var nextobj = obj.next('.field-indic');
        if (nextobj)
            nextobj.attr('class', 'field-indic');
    }
}
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function showLoading(obj) {
    if (obj) {
        var nextobj = obj.next('.field-indic');
        if (nextobj)
            nextobj.attr('class', 'field-indic loading-indicator');
    }
}


Mustache.tags = ['[[', ']]'];
var csrftoken = getCookie('csrftoken');
$(document).ready(function(){
    $.Mustache.addFromDom('tr_build_tmpl', 'tr_searched_pkgs_tmpl', 'depend_tmpl');
    $('#sel_os_release').change(function() {
        var sel_osr = $(this);
        if (!sel_osr.val()) {
            return;
        }
        $('.plugable').remove();
        $.ajax({
            type: 'post',
            url: '{%url ajax_package_builds %}',
            dataType: 'json',
            data: $('#form_package_query').serializeArray(),
            crossDomain: false,
            beforeSend: function(xhr, settings) {
                if (!csrfSafeMethod(settings.type)) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
                showLoading(sel_osr);
            },
            success: function(r) {
                endLoading(sel_osr);
                $('#tr_os_release').mustache(
                    'tr_build_tmpl', 
                    {m_builds: r.builds,
                    'm_builds_len?': r.builds ? r.builds.length : null},
                    {method: 'after'});
                },
            error: function() {
                endLoading(sel_osr);
                alert("get builds failed!");
            }
        }); // end of ajax
    }); // end of change

    $('#div_search').on('submit', '#form_package_query', function(e) {
        e.preventDefault();
        var sel_osr = $('#btn_search_package');
        $(".searched_pkgs").remove();
        $.ajax({
            type: 'post',
            url: '{%url ajax_search_package %}',
            dataType: 'json',
            data: $('#form_package_query').serializeArray(),
            crossDomain: false,
            beforeSend: function(xhr, settings) {
                if (!csrfSafeMethod(settings.type)) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
                showLoading(sel_osr);
            },
            success: function(r) {
                endLoading(sel_osr);
                $('#div_search table tbody').mustache(
                    'tr_searched_pkgs_tmpl', 
                    {pkg_list: r},
                    {method: 'append'});
                },
            error: function() {
                endLoading(sel_osr);
                alert("search package failed!");
            }
        }); // end of ajax
    }); // end of on

    $('#div_search').on('change', '#target_pkg', function(e) {
        e.preventDefault();
        $this = $(this);
        var sel_osr = $this;
        $(".pkg_list").remove();
        $.ajax({
            type: 'post',
            url: '{%url ajax_search_hierarchy %}',
            dataType: 'json',
            data: $('#form_package_query').serializeArray(),
            crossDomain: false,
            beforeSend: function(xhr, settings) {
                if (!csrfSafeMethod(settings.type)) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
                showLoading(sel_osr);
            },
            success: function(r) {
                endLoading(sel_osr);
                $('#bugzilla-body').mustache(
                    'depend_tmpl', 
                    {depends: r.depends,
                     'depends_len?': r.depends? r.depends.length : null,
                     affects: r.affects,
                     'affects_len?': r.affects? r.affects.length : null
                    },
                    {method: 'append'});
                },
            error: function() {
                endLoading(sel_osr);
                alert("search hierarchy failed!");
            }
        }); // end of ajax
    }); // end of on

}); // end of document ready
</script>
</head>
<body class="defect-opensolaris-org-bz">
{% include 'topnav.html' %}
<div>
  <table id="tabtitles" border="0" cellpadding="0" cellspacing="0">
  <tbody><tr>
    <td id="title">
    <p><font color="#FFFFFF">&nbsp;Package &raquo Query</font></p>
    </td></tr>
  </tbody></table>
</div>

<div id="bugzilla-body" style="overflow:auto; min-height: 450px;">
 

<div id="div_search" style="overflow: auto;">
  <form id="form_package_query" method="POST" action="">
  {% csrf_token %}
  <table border="0" align="left" class="bz_buglist">
     <tr id="tr_os_release">
      <td class="th-label">Solaris Release:</td>
      <td><select id="sel_os_release" name="sel_os_release" class="fixed-field">
      <option value=""></option>
      {% for os_release in os_release_list %}
      <option value="{{os_release.name}}">{{os_release.name}}</option>
      {% endfor %} 
      </select>
      <span class="field-indic"></span>
      </td>
     </tr>
  </table>
  </form>
</div>
<div><br></div>
</div>

{% include 'footer.html' %}
